package com.kha.listone.locale;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;

import com.kha.listone.util.Globals;

public class LocaleResources {

	private String[] localeStrings;

	public static LocaleResources getInstance() {
		return new LocaleResources();
	}

	public LocaleResources() {
		try {
			localeStrings = loadText(Globals.APP_LOCALE + ".res");
		} catch (IOException e) {
			localeStrings = null;
			e.printStackTrace();
		}
	}

	public String getString(short key) {
		if (localeStrings != null) {
			return localeStrings[key];
		} else {
			return "!!ERROR!!";
		}
	}

	public String[] loadText(String resName) throws IOException {
		String[] text = null;
		InputStream in = getClass().getResourceAsStream("/" + resName);
		try {
			DataInputStream din = new DataInputStream(in);
			int size = din.readShort();
			text = new String[size];
			for (int i = 0; i < size; i++) {
				text[i] = din.readUTF();
			}
			text[0] = text[0].substring(1, text[0].length());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			in.close();
		}
		return text;
	}
}
